package main

import (
	"log"
	"net/http"

	"github.com/go-martini/martini"
	"github.com/martini-contrib/render"
)

var (
	// 配置
	conf Conf
)

// func init() {}

func main() {
	conf = getconf()
	m := martini.Classic()
	m.Use(martini.Static("assets"))
	m.Use(render.Renderer(render.Options{
		Layout:    "layout",
		Directory: "templates",
		Charset:   "UTF-8",
		Delims:    render.Delims{"<go>", "</go>"},
	}))
	listRoutes(m)

	log.Println("Start Port: http://" + conf.Host)
	if err := http.ListenAndServe(conf.Host, m); err != nil {
		log.Fatal(err)
	}
}

func listRoutes(m *martini.ClassicMartini) {
	m.Get("/(?P<fname>[0-9A-Za-z_\\-/]*).(md|markdown)", readMd)
	m.Post("/(?P<fname>[0-9A-Za-z_\\-/]*).(md|markdown)", UpdateMd)
	m.Post("/create", UpdateMd)
	m.Get("/(?P<fname>[0-9A-Za-z_\\-/]*).(?P<fimg>png|jpg|jpeg|gif)", readImg)
	m.Get("/menus(?P<fdir>[0-9A-Za-z_\\-/]*)", readMenus)
	m.Get("/(?P<fdir>[0-9A-Za-z_\\-/]*)", readMenus)
}
